# 通过字典处理响应文本数据
# 获取天天基金网-基金-排行榜-纯债基金
#



import json
import re

import pandas as pd

# 响应文本数据
response_text = """
var rankData = {
    datas: ["003384,金鹰添盈纯债债券A,JYTYCZZQA,2024-04-09,1.2016,2.3102,0.03,0.14,0.23,1.05,1.7,140.14,142.83,112.63,1.08,147.56,2017-01-11,1,140.0843,0.80%,0.08%,1,0.08%,1,119.86", "012623,金鹰添盈纯债债券C,JYTYCZZQC,2024-04-09,1.1617,2.1282,0.03,0.13,0.23,1.04,1.69,134.9,135.42,,1.07,107.66,2021-06-08,1,134.8277,,0.00%,,,,", "008729,同泰恒利纯债C,TTHLCZC,2024-04-09,1.8179,2.3379,0.05,0.14,-0.03,73.33,74.29,124.98,129.07,138.31,73.33,142.4,2020-02-18,1,125.0057,,0.00%,,,,"],
    allRecords: 15247,
    pageIndex: 1,
    pageNum: 50,
    allPages: 305,
    allNum: 15247,
    zs_count: 2501,
    gp_count: 923,
    hh_count: 7397,
    zq_count: 3394,
    qdii_count: 193,
    fof_count: 837
};

response.text
"""

# 解析JSON格式字符串为Python字典

# 使用正则表达式或其他方法提取JSON字符串
# 注意：这里的示例直接使用了文本内容，因为在实际应用中可能需要更精确的方法来提取JSON字符串部分
start_index = response_text.find("{")
end_index = response_text.rfind("};") + 1  # 加1是为了包含结束的分号和括号
json_str = response_text[start_index:end_index]
print(json_str)

# 使用正则表达式给键添加双引号
json_str_fixed = re.sub(r'(\w+):', r'"\1":', json_str)
print(json_str_fixed)
# 解析JSON字符串为Python字典
parsed_data = json.loads(json_str_fixed)
# 尝试解析修正后的字符串
try:
    parsed_data = json.loads(json_str_fixed)
    print(parsed_data['datas'])
except json.JSONDecodeError as e:
    print(f"解析错误: {e}")


# 现在parsed_data是一个Python字典，您可以访问其中的数据
datas = parsed_data.get('datas', [])
all_records = parsed_data["allRecords"]
page_index = parsed_data["pageIndex"]
print(all_records)
print(page_index)
print(datas)
columns = [
    'fund_code', 'fund_name', 'fund_short_name', 'maturity_date',
    'current_nav', 'total_nav', 'daily_change', 'one_week_yield',
    'one_month_yield', 'three_month_yield', 'six_month_yield', 'one_year_yield',
    'two_year_yield', 'three_year_yield', 'year_to_date_yield', 'since_inception_yield',
    'issue_date', 'ji_shu1', 'since_inception_yield', 'purchase_fee_percent',
    'purchase_fee_discount', 'ji_shu2', 'purchase_fee_discount2', 'ji_shu3', 'other_field'
]
df = pd.DataFrame([data.split(',') for data in datas], columns=columns)
print(df.head(5))
